package com.czx.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czx.entity.DiscussPost;
import com.czx.entity.MyPage;
import com.czx.entity.User;
import com.czx.mapper.DisscussPostMapper;
import com.czx.common.security.UserTokenThreadLocal;
import com.czx.service.DiscussPostService;
import com.czx.common.utils.R;
import com.czx.common.utils.TimeUtils;
import com.czx.vo.DiscussPostVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;


@Service
public class DiscussPostServiceImpl extends ServiceImpl<DisscussPostMapper, DiscussPost> implements DiscussPostService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    //首页帖子列表
    @Override
    public MyPage disCussPage(MyPage page,Integer orderMode) {
        MyPage myPage = baseMapper.disCussPage(page,orderMode);
        List<DiscussPostVo> records = myPage.getRecords();
        for (DiscussPostVo record : records) {
            record.setPost(TimeUtils.format(record.getCreateTime()));
        }
        return myPage;
    }

    @Override
    public DiscussPostVo disDetail(Integer id) {
        //获取帖子详情
        DiscussPostVo discussPostVo =baseMapper.disDetail(id);
        String time = TimeUtils.format(discussPostVo.getCreateTime());
        discussPostVo.setPost(time);
        return discussPostVo;
    }


    /**
     * 搜索帖子
     * @param keyword
     * @param page
     * @return
     */
    @Override
    public MyPage searchPost(String keyword, MyPage page) {
        MyPage myPage = baseMapper.searchPost(keyword, page);
        return myPage;
    }

    /**
     * 更新帖子分数
     * @param postId
     * @param score
     */
    @Override
    public void updatePostScore(Integer postId, Double score) {
        //更新帖子分数
        DiscussPost discussPost = baseMapper.selectById(postId);
        discussPost.setScore(score);
        baseMapper.updateById(discussPost);
    }

    /**
     * 置顶或取消置顶
      * @param postId
     * @param postType
     */
    @Override
    public void top(Integer postId, Integer postType) {
        // 根据postId和postType进行置顶或取消置顶操作
        baseMapper.updateTop(postId, postType);

    }

    /**
     * 加精
     * @param postId
     */
    @Override
    public void wonderful(Integer postId) {
       baseMapper.setWonderful(postId);
    }

    /**
     * 删除
     * @param postId
     */
    @Override
    public void delete(Integer postId) {
        baseMapper.setDelete(postId);
    }

    /**
     * 修改后台帖子管理
     * @param postDto
     * @return
     */
    /*@Override
    public boolean updatePost(PostDto postDto) {
       int row= baseMapper.updatePost(postDto);
        return row>0;
    }*/

    //获取我的帖子
    @Override
    public MyPage getPostId(MyPage page, Integer id, Integer orderMode) {
        if (id == null) {
            id= UserTokenThreadLocal.get().getUser().getId();
        }
        if (orderMode == null) {
            orderMode=0;
        }
      MyPage myPage=  baseMapper.getPostId(page,id,orderMode);
      List<DiscussPostVo>  records = myPage.getRecords();
        for (DiscussPostVo record : records) {
            System.out.println(record);
            record.setPost(TimeUtils.format(record.getDiscussPost().getCreateTime()));
            System.out.println(record.getPost());
        }
        return myPage;

    }

    /**
     * 通过帖子id查询帖子信息
     * @param postId
     * @return
     */
    public DiscussPost getDiscussPostById(Integer postId){
        return baseMapper.selectById(postId);
    }

    /**
     * 给帖子评论的数量+1
     */
    public void countByOne(Integer postId){
        DiscussPost discussPost = getDiscussPostById(postId);
        discussPost.setCommentCount(discussPost.getCommentCount()+1);
        baseMapper.updateById(discussPost);
    }


    /**
     * 加添帖子
     * @param user
     * @param discussPost
     * @return
     */
    @Override
    public R addDisscuss(User user, DiscussPost discussPost) {
        discussPost.setUserId(user.getId());
        discussPost.setStatus(0);
        discussPost.setCreateTime(new Date());
        discussPost.setType(0);
        baseMapper.insert(discussPost);
        return R.success();
    }

}
